home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7183 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: simple code, argc, argv, strcmp()
  5. Date: 16 Feb 96 20:19:36 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.824501976@rscernix>
  8. References: <11f7cc$17261a.3b3@daprez> <4g1vl1$rm2@maverick.tad.eds.com>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4g1vl1$rm2@maverick.tad.eds.com> Ed McGuffey <fgae23@ods04.and.ifg.gmeds.com> writes:
  13.                                         ^^^^^^^^
  14. I have misspelled McGoofey ;-)  Sorry for the tasteless joke, couldn't
  15. resist.
  16.  
  17. >If you really want to use the !, try:
  18. >   if (!((strcmp(argv[1], "-d") || (strcmp(argv[1], "-e"))) 
  19. >      Usage();
  20. >
  21. >since you want the ! to apply to the entire expression consisting of the
  22. >two compares.  The code is more readable, however, to avoid negative logic
  23. >altogether and just say:
  24. >
  25. >   if ((strcmp(argv[1], "-d") || (strcmp(argv[1], "-e")) 
  26.  
  27. Your parentheses don't match.  Why? :-)
  28.  
  29. >      function();
  30. >   else
  31. >      Usage();
  32.  
  33. Oh no!  There is no way to stop people from posting their own broken
  34. versions of the original broken version of the code :-(
  35.  
  36. The above one will ALWAYS call function(), because it is IMPOSSIBLE
  37. that BOTH strcmp(argv[1], "-d") AND (strcmp(argv[1], "-e") return 0.
  38.  
  39. Don't play smart and suppress the == or != operators from conditional
  40. expressions, even if they _can_ be suppressed.  If you use them, the
  41. chances of getting it right are considerably increased, e.g.
  42.  
  43.     if (strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "-e") == 0)
  44.     function();
  45.     else
  46.     Usage();
  47.  
  48. is considerably more readable than the equivalent:
  49.  
  50.     if (!strcmp(argv[1], "-d") || !strcmp(argv[1], "-e"))
  51.     ...
  52.  
  53. or the De Morgan'ized version of the above:
  54.  
  55.     if (!(strcmp(argv[1], "-d") && strcmp(argv[1], "-e")))
  56.     ...
  57.  
  58. Dan
  59. --
  60. Dan Pop
  61. CERN, CN Division
  62. Email: danpop@mail.cern.ch 
  63. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  64.